﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmLocationGoodsMove : Form
    {
        public FrmLocationGoodsMove()
        {
            InitializeComponent();
        }

        private static List<LocationGoodsInfo> _lstLocationGoods = null;
        public static List<LocationGoodsInfo> lstLocationGoods
        {
            get
            {
                if (_lstLocationGoods == null) _lstLocationGoods = new List<LocationGoodsInfo>();
                return _lstLocationGoods;
            }
        }
        private static LocationGoodsInfo _LocationGoods = new LocationGoodsInfo();

        private void FrmLocationGoods_Load(object sender, EventArgs e)
        {

        }
        int index = 1;
        //提交
        private void btnOK_Click(object sender, EventArgs e)
        {

            try
            {

                if (string.IsNullOrEmpty(txtDestLocationCode.Text))
                {
                    MessageBox.Show("目的货位不能为空！");
                    this.txtDestLocationCode.Focus();
                    return;
                }

                Cursor.Current = Cursors.WaitCursor;

                if (lstLocationGoods != null && lstLocationGoods.Count > 0)
                {

                    var re = Duty.LocationGoodsUIRule.NewInstance.LocationGoodsMove(LoginBean.Instance.USERCODE, txtDestLocationCode.Text, lstLocationGoods);


                    if (re.Params)
                    {
                        MessageBox.Show("提交成功！");
                        _lstLocationGoods = null;
                        this.LocationGoodsSource.DataSource = null;
                        this.dbGridLocationGoods.DataSource = this.LocationGoodsSource;
                        this.dbGridLocationGoods.Refresh();
                        this.txtGoodsCode.Text = string.Empty;
                        this.txtLocationCode.Text = string.Empty;
                        this.txtDestLocationCode.Text = string.Empty;
                        //this.txtQTY.Text = string.Empty;
                        this.txtGoodsCode3.Text = string.Empty;
                        this.txtGoodsCode.Focus();

                    }
                    else
                    {
                        MessageBox.Show(re.Message);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        //上架商品扫描验证
        private void txtGoodsCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            try
            {
                string GoodsCode = this.txtGoodsCode.Text.Trim();
                if (string.IsNullOrEmpty(GoodsCode))
                {
                    MessageBox.Show("扫描上架商品条码不能为空！");
                    this.txtGoodsCode.Focus();
                    return;
                }
                if (GoodsCode.Length < 10)
                {
                    MessageBox.Show("上架商品条码不能小于10位数！");
                    this.txtGoodsCode.SelectAll();
                    return;
                }
                if (GoodsCode.Substring(0, 1) != '0'.ToString() && GoodsCode.Substring(0, 3) != "WOC".ToString())
                {
                    MessageBox.Show("请扫描‘0’或‘WOC’开头的商品条码！");
                    this.txtGoodsCode.SelectAll();
                    return;
                }
                 
                Cursor.Current = Cursors.WaitCursor;
                //_LocationGoods.GOODSCODE = GoodsCode.Substring(0,10);
                this.txtGoodsCode3.Focus();
                this.txtGoodsCode3.SelectAll();
            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex); 
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        //下架商品编码扫描验证
        private void txtGoodsCode3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            try
            {
                string GoodsCode3 = this.txtGoodsCode3.Text.Trim();
                if (string.IsNullOrEmpty(GoodsCode3))
                {
                    MessageBox.Show("扫描下架商品条码不能为空！");
                    this.txtGoodsCode.Focus();
                    return;
                }
                if (GoodsCode3.Length < 10)
                {
                    MessageBox.Show("下架商品条码不能小于10位数！");
                    this.txtGoodsCode3.SelectAll();
                    return;
                }

                //_lstLocationGoods = Duty.LocationGoodsUIRule.NewInstance.GetLocationALLGoods(txtGoodsCode.Text, txtGoodsCode3.Text, txtLocationCode.Text);

                //if (_LocationGoods == null)
                //{

                //    if (!string.IsNullOrEmpty(txtQTY.Text))
                //    {
                //        MessageBox.Show("请输入搬移数量！");
                //        this.txtQTY.Focus();
                //        return;
                //    }
                //    _LocationGoods.LOCATIONCODE = txtLocationCode.Text;
                //    _LocationGoods.QTY = Convert.ToDecimal(txtQTY.Text);
                //    _LocationGoods.GOODSCODE = txtGoodsCode.Text;
                   
                //}
                //this.LocationGoodsSource.DataSource = null;
                //this.LocationGoodsSource.DataSource = lstLocationGoods;
                //this.dbGridLocationGoods.DataSource = this.LocationGoodsSource;
                //this.dbGridLocationGoods.Refresh();



                Cursor.Current = Cursors.WaitCursor;
                //_LocationGoods.GOODSCODE3 = GoodsCode3.Substring(0,10);
                this.txtLocationCode.Focus();
                this.txtLocationCode.SelectAll();
            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);

            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        //货位扫描验证
        private void txtLocationCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            try
            {
                string GoodsCode = this.txtGoodsCode.Text.Trim();

                if (string.IsNullOrEmpty(GoodsCode))
                {
                    MessageBox.Show("请先扫描上架商品条码！");
                    this.txtGoodsCode.Focus();
                    return;
                }
                if (GoodsCode.Substring(0, 1) != '0'.ToString() && GoodsCode.Substring(0, 3) != "WOC".ToString())
                {
                    MessageBox.Show("请扫描‘0’或‘WOC’开头的商品条码！");
                    this.txtGoodsCode.SelectAll();
                    return;
                }
                if (GoodsCode.Length < 10)
                {
                    MessageBox.Show("上架商品条码不能小于10位数！");
                    this.txtGoodsCode.Focus();
                    this.txtGoodsCode.SelectAll();
                    return;
                
                }
                if (GoodsCode.Length >= 17)
                    GoodsCode = GoodsCode.Substring(0,GoodsCode.Length - 8);
                string GoodsCode3 = this.txtGoodsCode3.Text.Trim();
                if (string.IsNullOrEmpty(GoodsCode3))
                {
                    MessageBox.Show("请先扫描下架商品条码！");
                    txtGoodsCode3.Focus();
                    return;
                }
                if (GoodsCode3.Length < 10)
                {
                    MessageBox.Show("下架商品条码不能小于10位数！");
                    this.txtGoodsCode3.Focus();
                    this.txtGoodsCode3.SelectAll();
                    return;
                }
                string LocationCode = this.txtLocationCode.Text.Trim();
                if (string.IsNullOrEmpty(LocationCode))
                {
                    MessageBox.Show("请扫描原货位编码！");
                    this.txtLocationCode.Focus();
                    return;
                }
                if (GoodsCode3.Length >= 17)
                    GoodsCode3 = GoodsCode3.Substring(0,GoodsCode3.Length - 8);
                Cursor.Current = Cursors.WaitCursor;
                //_LocationGoods.GOODSCODE = GoodsCode.Substring(0, 10);
                //_LocationGoods.GOODSCODE3 = GoodsCode3.Substring(0, 10);
                //_LocationGoods.LOCATIONCODE = LocationCode;
                //if (lstLocationGoods.FindIndex(x => x.GOODSCODE == _LocationGoods.GOODSCODE) < 0)
                //{
                //    lstLocationGoods.Add(_LocationGoods);
                //}
                //else
                //{
                //    MessageBox.Show("该上架商品已添加！");
                //    txtGoodsCode.Focus();
                //    txtGoodsCode.SelectAll();
                //    return;
                //}
                _lstLocationGoods = Duty.LocationGoodsUIRule.NewInstance.GetLocationALLGoods(GoodsCode, GoodsCode3, txtLocationCode.Text);
                this.LocationGoodsSource.DataSource = null;
                this.LocationGoodsSource.DataSource = lstLocationGoods;
                this.dbGridLocationGoods.DataSource = this.LocationGoodsSource;
                this.dbGridLocationGoods.Refresh();
                _LocationGoods = new LocationGoodsInfo();
                this.txtDestLocationCode.Focus();

            } 
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        
        private void FrmLocationGoods_Closed(object sender, EventArgs e)
        {
            //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
            //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
            //Object o = Activator.CreateInstance(type);
            //System.Reflection.MethodInfo mi = type.GetMethod("Show");

            //mi.Invoke(o, null);
            lstLocationGoods.Clear();
            
            this.Close();
            this.Dispose();
        }

        private void FrmLocationGoods_Closing(object sender, CancelEventArgs e)
        {
            if (MessageBoxEx.Question("退出将清空数据，是否退出？") == DialogResult.No)
            {
                e.Cancel = true;
            }
            
        }
        
        //双击删除数据
        private void dbGridLocationGoods_DoubleClick(object sender, EventArgs e)
        {
            //int curIndex = this.dbGridLocationGoods.CurrentRowIndex;
            //if (curIndex < 0) return;
            //try
            //{
            //    string goodsBatchCode = dbGridLocationGoods[curIndex, 0].ToString();
            //    LocationGoodsInfo curItem = lstLocationGoods.Find(x => x.GOODSBATCHCODE == goodsBatchCode);

            //    string ltBatchCode = curItem.GOODSBATCHCODE;
            //    decimal ltQTY = curItem.QTY;
               
            //    string Message = string.Format("批次号:{0}, 库存数量为:{1} \n 是否删除该数据？", ltBatchCode, ltQTY);
            //    if (MessageBoxEx.Question(Message) == DialogResult.Yes)
            //    {
            //        lstLocationGoods.Remove(curItem);
            //        this.LocationGoodsSource.DataSource = null;
            //        this.LocationGoodsSource.DataSource = lstLocationGoods;
            //        this.dbGridLocationGoods.DataSource = this.LocationGoodsSource;
            //        this.dbGridLocationGoods.Refresh();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    TraceEx.Write(ex.Message);
            //    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            //}
            //finally
            //{

            //}

        }

        //private void txtQTY_KeyDown(object sender, KeyEventArgs e)
        //{
        //     if (e.KeyCode != Keys.Enter || string.IsNullOrEmpty(txtQTY.Text)) return;
        //     //int QTY = 0;
        //     //if (!int.TryParse(txtQTY.Text, out QTY))
        //     //    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("请数字类型!");
           
        //     int curIndex = this.dbGridLocationGoods.CurrentRowIndex;
        //     if (e.KeyCode != Keys.Enter) return;

        //     string GoodsCode = this.txtGoodsCode.Text.Trim();

        //     if (string.IsNullOrEmpty(GoodsCode))
        //     {
        //         MessageBox.Show("请先扫描上架商品条码！");
        //         this.txtGoodsCode.Focus();
        //         return;
        //     }
        //     if (GoodsCode.Substring(0, 1) != '0'.ToString())
        //     {
        //         MessageBox.Show("请扫描‘0’开头的商品条码！");
        //         this.txtGoodsCode.SelectAll();
        //         return;
        //     }
        //     if (GoodsCode.Length < 10)
        //     {
        //         MessageBox.Show("上架商品条码不能小于10位数！");
        //         this.txtGoodsCode.Focus();
        //         this.txtGoodsCode.SelectAll();
        //         return;
        //     }
        //     string GoodsCode3 = this.txtGoodsCode3.Text.Trim();
        //     if (string.IsNullOrEmpty(GoodsCode3))
        //     {
        //         MessageBox.Show("请先扫描下架商品条码！");
        //         txtGoodsCode3.Focus();
        //         return;
        //     }
        //     if (GoodsCode3.Length < 10)
        //     {
        //         MessageBox.Show("下架商品条码不能小于10位数！");
        //         this.txtGoodsCode3.Focus();
        //         this.txtGoodsCode3.SelectAll();
        //         return;
        //     }
        //     try
        //     {
        //         if (curIndex < 0 || lstLocationGoods.Count() <= 0)
        //         {
        //             _LocationGoods = Duty.LocationGoodsUIRule.NewInstance.LocationGoodsInfoByCode(txtGoodsCode.Text.Substring(0, 10), txtGoodsCode3.Text.Substring(0, 10));
                   
        //              if (_LocationGoods==null)
        //              {
        //                  MessageBox.Show("找不到该商品信息");
 
        //                  return;
        //              }
        //             _LocationGoods.QTY = Convert.ToDecimal(txtQTY.Text);
        //             lstLocationGoods.Add(_LocationGoods);
        //             this.LocationGoodsSource.DataSource = null;
        //             this.LocationGoodsSource.DataSource = lstLocationGoods;
        //             this.dbGridLocationGoods.DataSource = this.LocationGoodsSource;
        //             this.dbGridLocationGoods.Refresh();

        //         }
        //         else
        //         {
        //             string goodsBatchCode = dbGridLocationGoods[curIndex, 0].ToString();
        //             int originalQTY =Convert.ToInt32( dbGridLocationGoods[curIndex, 1]);
        //             //if (Convert.ToInt32(txtQTY.Text) > originalQTY)
        //             //{
        //             //    MessageBox.Show("数量不能大于原有库存！");
        //             //    this.txtQTY.Focus();
        //             //    return;
        //             //}
        //             LocationGoodsInfo curItem = lstLocationGoods.Find(x => x.GOODSBATCHCODE == goodsBatchCode);
        //             LocationGoodsInfo curNetItem = curItem;
        //             lstLocationGoods.Remove(curItem);
        //             curNetItem.QTY = Convert.ToDecimal(txtQTY.Text);

        //             lstLocationGoods.Add(curNetItem);
        //             this.LocationGoodsSource.DataSource = null;
        //             this.LocationGoodsSource.DataSource = lstLocationGoods;
        //             this.dbGridLocationGoods.DataSource = this.LocationGoodsSource;
        //             this.dbGridLocationGoods.Refresh();
        //         }
                
        //     }
        //     catch (Exception ex)
        //     {
        //         TraceEx.Write(ex.Message);
        //         WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("修改数量失败!");
        //     }
              
        //}

        private void txtDestLocationCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter || string.IsNullOrEmpty(txtDestLocationCode.Text)) return;

            txtDestLocationCode.Focus();
        }

        private void dbGridLocationGoods_CurrentCellChanged(object sender, EventArgs e)
        {
            //this.dbGridLocationGoods.SelectionBackColor = Color.Green; 
            //this.dbGridLocationGoods.SelectionBackColor=
            this.dbGridLocationGoods.Select(this.dbGridLocationGoods.CurrentRowIndex);
        }

     
       

       
    }
}